﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class StudInfoConfirm : BasePage
{
    string _timeID = string.Empty;
    protected override void Page_LoadAlways()
    {
        base.Page_LoadAlways();
        if (Request.QueryString.Count > 0)
        {
            _timeID = Request.Params["TimeID"].Trim();
            //判断是否超时
            if (Exam.ExamTime_IsTimeOut(_timeID))
            {
                #region 如果已有报名信息
                DataTable dt_apply = Exam.ApplyInfo_GetInfo(IDCard, _timeID);
                if (dt_apply.Rows.Count > 0)
                {
                    DataRow dr_apply = dt_apply.Rows[0];
                    string orderId = dr_apply["OrderID"].ToString();
                    string sOrderId = security.EncryptQueryString(orderId);
                    int applyState = int.Parse(dr_apply["ApplyState"].ToString());
                    if (applyState == 1)
                    {
                        //Response.Redirect("BM4.aspx?orderID=" + sOrderId);
                        Response.Redirect("OrderInfoConfirm.aspx?orderID=" + sOrderId);
                    }
                    else if (applyState == 2)
                    {
                        Response.Redirect("BM5.aspx?orderID=" + sOrderId);
                    }
                }
                #endregion

            }
            else
                Response.Redirect(BSUtility.mainUrl + "Default.aspx");
        }
        else
        {
            Response.Redirect(BSUtility.mainUrl + "default.aspx");
        }
    }
    protected override void Page_LoadOnce()
    {
        base.Page_LoadOnce();
        DataTable dt = StudInfo_Control.getStudInfoByStudIdCard(IDCard);
        NameText.Text = UserName;
        SFZHText.Text = IDCard;
        if (dt.Rows.Count == 1)
        {
            DataRow dr = dt.Rows[0];
            if (dr["StudSex"].ToString().Trim() == "女")
            {
                rbMan.Checked = false;
                rbMale.Checked = true;
            }
            tbYear.Text = dr["StudYear"].ToString();
            SIDText.Text = dr["StudID"].ToString();
            ClassText.Text = dr["StudClassName"].ToString();
            DepText.Text = dr["StudDeptName"].ToString();
            FromText.Text = dr["StudFrom"].ToString();
            tbPhone.Text = dr["StudTel"].ToString();
        }
    }
    protected void Page_PreRender()
    {
        string photoUrl = BSUtility.mainUrl + "StudPhoto/" + IDCard.Trim() + ".jpg";
        string address = Server.MapPath(photoUrl);
        if (File.Exists(address))
        {
            userPic.Visible = true;
            userPic.ImageUrl = photoUrl;
            //校外的学生允许自己上传照片
            if (SchoolFlag)
                fileUp.Visible = false;
            else
                fileUp.Visible = true;
        }
        else
        {
            userPic.Visible = false;
            //校外的学生允许自己上传照片
            if (SchoolFlag)
                fileUp.Visible = false;
            else
                fileUp.Visible = true;
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string picUrl = Server.MapPath(BSUtility.mainUrl + "StudPhoto/" + IDCard.Trim() + ".jpg");
        if (File.Exists(picUrl))
        {
            string studSex = (rbMan.Checked == true) ? "男" : "女";
            DataTable dt = StudInfo_Control.getStudInfoByStudIdCard(IDCard);
            if (dt.Rows.Count > 0)
            {
                DataRow dr = dt.Rows[0];
                string userPic = dr["StudPic"].ToString();
                string[] studInfo = new string[] { IDCard, UserName.Trim(), studSex, DepText.Text.Trim(), ClassText.Text.Trim(), userPic, "0", SIDText.Text.Trim(), FromText.Text.Trim(), tbPhone.Text.Trim(), tbYear.Text.Trim() };
                if (StudInfo_Control.StudInfo_Save(studInfo, IDCard))
                {
                    //Response.Redirect("BM2.aspx?EmYear=" + sEmYear + "&EmNum=" + sEmNum + "&EmID=" + sEmId);
                    Response.Redirect("BMInfoFill.aspx?TimeID=" + _timeID);
                }
                else
                {
                    BSUtility.WindowAlert(this, "抱歉，请重试!");
                }
            }
        }
        else
            BSUtility.WindowAlert(this, "抱歉，系统没有您的照片，您必须去上传您的照片之后才允许报名。");
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (fileUpload.HasFile)
        {
            string fileName = fileUpload.FileName;
            string ext = Path.GetExtension(fileName).ToLower();
            if (ext == ".jpg")
            {
                //用FileUpload.FileName属性得到上传文件名，也可以使用HttpPostedFile.FileName得到。  
                string filePath = BSUtility.mainUrl + "StudPhoto/" + IDCard + ext;
                //MapPath方法,检索虚拟路径（绝对的或相对的）或应用程序相关的路径映射到的物理路径。  
                //FileUpload.SavaAs()方法用于把上传文件保存到文件系统中，也可以使用HttpPostedFile.SaveAs()方法。  
                fileUpload.SaveAs(MapPath(filePath));
            }
            else
                lbUpInfo.Text = "只允许上传jpg格式的文件";
        }
        else
            lbUpInfo.Text = "您需要选择一个文件";
    }
}
